var	urlHead = "/International";
//variables变量
var setMajorUrl = urlHead + "/teaTt/getKb"; //获取课表
var mystuUrl = urlHead + "/mystu/index?uuid="; //获取课表

function O(_id){
	return document.getElementById(_id);
}
//改变select选中状态
function setSelectChecked(selectId, checkValue){  
    var select = O(selectId);  
    for(var i=0; i<select.options.length; i++){  
        if(select.options[i].innerHTML == checkValue){  
            select.options[i].selected = true;  
            break;  
        }  
    }  
}; 
function setMajor(){

	var termValue = O("termValue").value;
	$.ajax({
     	type:"POST",
     	url:setMajorUrl,
     	dataType:"json",
     	data:{
     		termValue:termValue
     	},
     	success:function(data){renderKB(data);}, 
     	error:function(err){
            alert("123456"+err);
            alert("err");
        },
     });  
	
}
var seq = {1 : 'uuid', 2 : 'termValue', 3 : 'majorNo', 4 : 'majorName', 5 : 'courseNo', 6 : 'courseName', 7 : 'tNo', 8 : 'tName',
           9 : 'classroomNo', 10 : 'startLesson', 11 : 'startWeek', 12 : 'endlesson', 13 : 'endWeek', 14 : 'whichDay'}

var weekseq = {1 : '星期一', 2 : '星期二', 3 : '星期三', 4 : '星期四', 5 : '星期五'}

function renderKB(data){
	var week = new Array();
	//var weekseq = {1 : 'Monday', 2 : 'Tuesday', 3 : 'Wednesday', 4 : 'Thursday', 5 : 'Friday'}
	for(var key in weekseq){
		week[weekseq[key]] = new Array();
	}
    var innerHtml = ''
	
	if(data != null){
		for(var i = 0; i < data.length; i++){
			switch(data[i]['whichDay']){
				case weekseq[1]:
					week[weekseq[1]].push(data[i]);
					break;
				case weekseq[2]:
					week[weekseq[2]].push(data[i]);
					break;
				case weekseq[3]:
					week[weekseq[3]].push(data[i]);
					break;
				case weekseq[4]:
					week[weekseq[4]].push(data[i]);
					break;
				case weekseq[5]:
					week[weekseq[5]].push(data[i]);
					break;
			}
    	}
	}
    
	
	//alert(week[weekseq[2]][0]['whichDay'])
	innerHtml = calculateKB(week)
	var kb = O('kbtable')
	kb.innerHTML = innerHtml;
}
//计算课表布局
function calculateKB(week){
	//var kb = $('#kbtable')
	var col = new Array();
	for(var key in weekseq){
		col[weekseq[key]] = new Array();
	}
	for(var key in week){
		var flag = false;
		for(var i = 0; i < week[key].length; i++){
			var rowspan = parseInt(week[key][i]['endLesson']) - parseInt(week[key][i]['startLesson']) + 1;
			var td = '<td rowspan="' + rowspan + '" class="clickable" onclick="window.location.href=\'' + mystuUrl+week[key][i]['uuid'] + '\'">' + week[key][i]['courseName'] 
				+ '<br>(week ' + week[key][i]['startWeek'] + '-'+ week[key][i]['endWeek'] + ')<br>Location: ' + week[key][i]['classroomNo'] + '</td>';
			flag = false;
			for(var course = 0; course < col[key].length; course++){
				if(flag){
					break;
				}
				for(var offset = 0; offset < rowspan; offset++){
					if(col[key][course][parseInt(week[key][i]['startLesson']) + offset] == undefined){
						if(offset == rowspan - 1){
							col[key][course][parseInt(week[key][i]['startLesson'])] = td
							//合并的单元格用空格代替
							for(var placeholder = 1; placeholder < rowspan; placeholder++){
								col[key][course][parseInt(week[key][i]['startLesson']) + placeholder] = ' '
							}
							flag = true;
						}
					}
					else{
						flag = false;
						break;
					}
					
				}
			}
			if(flag == false){
				//保存一列，便于计数，从1开始
				var row = new Array(13);
				row[parseInt(week[key][i]['startLesson'])] = td
				//合并的单元格用空格代替
				for(var placeholder = 1; placeholder < rowspan; placeholder++){
					row[parseInt(week[key][i]['startLesson']) + placeholder] = ' '
				}
				col[key].push(row)
			}
		}
	}
	
	var innerHtml = '<table class="table table-bordered">\
					 	<thead>\
						 	<tr>\
							 	<th>时间段</th>\
								<th>节次</th>\
								<th colspan="' + (col["星期一"].length > 1 ? col["星期一"].length : 1)  + '">星期一</th>\
								<th colspan="' + (col["星期二"].length > 1 ? col["星期二"].length : 1)  + '">星期二</th>\
								<th colspan="' + (col["星期三"].length > 1 ? col["星期三"].length : 1)  + '">星期三</th>\
								<th colspan="' + (col["星期四"].length > 1 ? col["星期四"].length : 1)  + '">星期四</th>\
								<th colspan="' + (col["星期五"].length > 1 ? col["星期五"].length : 1)  + '">星期五</th>\
							</tr>\
						</thead>\
						<tbody>'
	var end = '</tbody>\
			</table>'
	var space = 0;
	//绘制课表时间段和节次
	for(var i = 1; i < 13; i++){
		var line = ''
		switch(i){
			case 1 : line = '<tr><th rowspan="5"></br></br></br></br>上午</th><th>' + i + '</th>'
				break;
			case 6 : line = '<tr><th rowspan="4"></br></br></br>下午</th><th>' + i + '</th>'
				break;
			case 10: line = '<tr><th rowspan="3"></br></br>\
			晚上</th><th>' + i + '</th>'
				break;
			default: line = '<tr><th>' + i + '</th>'
		}
		for(var key in weekseq){
			if(col[weekseq[key]].length == 0){
				line += '<td></td>'
			}
			else{
				for(var j = 0; j < col[weekseq[key]].length; j++){
					if(col[weekseq[key]][j][i] != undefined){
						line += col[weekseq[key]][j][i];
					}
					else{
						line += '<td></td>'
					}
				}
			}
		}
		line += '</tr>';
		innerHtml += line;
	}
	
	return innerHtml + end;
}


//第一次打开页面先显示一张课表
setMajor();
